package com.shioirikukuri.service.impl;

import com.shioirikukuri.common.exception.BusinessException;
import com.shioirikukuri.common.web.CommonCodeMsg;
import com.shioirikukuri.domain.SeckillProduct;
import com.shioirikukuri.domain.SeckillProductVo;
import com.shioirikukuri.mapper.SeckillProductMapper;
import com.shioirikukuri.redis.SeckillRedisKey;
import com.shioirikukuri.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by shioirikukuri
 */
@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public List<SeckillProduct> queryByTime(Integer time) {
        /*
        String key = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString());
        List<String> idList = redisTemplate.opsForList().range(key, 0, -1);
        if(idList.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        List<String> keyList = idList.stream().map(id -> SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(id)).collect(Collectors.toList());
        List<SeckillProduct> seckillProductList = redisTemplate.opsForValue().multiGet(keyList).stream().filter(objStr -> objStr != null).map(objStr -> JSON.parseObject(objStr, SeckillProduct.class)).collect(Collectors.toList());
        return seckillProductList;
        */
        //从redis中查询
        String seckillProductListKey = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString());
        List<String> idList = redisTemplate.opsForList().range(seckillProductListKey, 0, -1);
        /*
        List<SeckillProduct> seckillProductList = idList.stream().map(id -> {
            String detailKey = SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(id);
            return JSON.parseObject(redisTemplate.opsForValue().get(detailKey),SeckillProduct.class);
        }).collect(Collectors.toList());
        */


        //[1,2,3,4,5]
        //["seckillProductDetail:1","seckillProductDetail:2",...]
        //["1号商品对象JSON字符串",...]
        //[1号商品对象,2号商品对象,3号商品对象,4号商品对象,5号商品对象]
        List<String> detailKeyList = idList.stream().map(id -> SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(id)).collect(Collectors.toList());
        List<SeckillProduct> seckillProductList = redisTemplate.opsForValue().multiGet(detailKeyList).stream().map(objStr -> JSON.parseObject(objStr, SeckillProduct.class)).collect(Collectors.toList());

        return seckillProductList;
    }

    @Override
    public SeckillProductVo find(Long seckillId) {
        //从redis中查询
        /*String key = SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(seckillId.toString());
        String objStr = redisTemplate.opsForValue().get(key);
        if(objStr==null){
            throw new BusinessException(CommonCodeMsg.ILLEGAL_OPERATION);
        }
        return JSON.parseObject(objStr,SeckillProductVo.class);*/
        String detailKey = SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(seckillId.toString());
        return JSON.parseObject(redisTemplate.opsForValue().get(detailKey),SeckillProductVo.class);

    }

    @Override
    public SeckillProductVo find(Integer time,Long seckillId) {
        String key = SeckillRedisKey.SECKILL_PRODUCT_DETAIL.getRealKey(seckillId.toString());
        String objStr = redisTemplate.opsForValue().get(key);
        if(objStr==null){
            throw new BusinessException(CommonCodeMsg.ILLEGAL_OPERATION);
        }
        return JSON.parseObject(objStr,SeckillProductVo.class);
        /*
        SeckillProduct seckillProduct = seckillProductMapper.find(seckillId);
        Result<Product> result = productFeignApi.find(seckillProduct.getProductId());
        if(result == null || result.hasError()){
            throw new BusinessException(CommonCodeMsg.PRODUCT_SERVER_ERROR);
        }
        Product product = result.getData();
        SeckillProductVo vo = new SeckillProductVo();
        BeanUtils.copyProperties(product,vo);
        BeanUtils.copyProperties(seckillProduct,vo);
        return vo;
        */
    }

    /**
     * 减少库存,返回影响的行数
     * @param seckillId
     * @return
     */
    @Override
    public int decrStockCount(Long seckillId) {
        return seckillProductMapper.decrStock(seckillId);
    }



    @Override
    public void incrStockCount(Long seckillId) {
        seckillProductMapper.incrStock(seckillId);
    }

    @Override
    public List<SeckillProduct> queryTodayData() {
        return seckillProductMapper.querySeckillProduct();
    }
}
